#!/usr/bin/ruby
# Copyright:: Copyright 2009 Google Inc.
# Original Author:: Ryan Brown (mailto:ribrdb@google.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module AppEngine
  module SDK
    VERSION = '<%= version %>'
    SDK_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..',
                                          '<%= SDK_ROOT %>'))
    TOOLS_JAR = File.join(SDK_ROOT, *%w{lib appengine-tools-api.jar})
    API_JAR = File.join(SDK_ROOT, *%w{lib impl appengine-api.jar})
    LABS_JAR = File.join(SDK_ROOT, *%w{lib impl appengine-api-labs.jar})
    AGENT_JAR = File.join(SDK_ROOT, *%w{lib agent appengine-agent.jar})
    RUNTIME_JARS = [
      File.join(SDK_ROOT, *%w{lib shared appengine-local-runtime-shared.jar}),
      File.join(SDK_ROOT, *%w{lib impl appengine-api-stubs.jar}),
      File.join(SDK_ROOT, *%w{lib impl appengine-local-runtime.jar}),
      File.join(SDK_ROOT, *%w{lib testing appengine-testing.jar}),
      ]

    if defined?(Java)
      class << self

        # Tries to load the ApiProxy class.
        def load_apiproxy
          with_jars(API_JAR) do
            return Java.ComGoogleApphostingApi.ApiProxy
          end
        end
        
        def load_labs
          with_jars(LABS_JAR) do
            return Java.ComGoogleAppengineApiLabsTaskqueue.QueueFactory
          end
        end
      
        # Tries to load the LocalServiceTestHelper class.
        def load_local_test_helper
          load_apiproxy
          load_labs
          with_jars(*RUNTIME_JARS) do
            return Java.ComGoogleAppengineToolsDevelopmentTesting.LocalServiceTestHelper
          end
        end
      
        # Tries to load the Kickstart class.
        def load_kickstart
          cp = java.lang.System.get_property('java.class.path')
          sep = java.io.File::pathSeparator
          java.lang.System.set_property('java.class.path',
                                        "#{TOOLS_JAR}#{sep}#{cp}")
          with_jars(TOOLS_JAR) do
            return Java.ComGoogleAppengineTools.KickStart
          end
        end

        def load_appcfg
          with_jars(TOOLS_JAR) do
            return Java.ComGoogleAppengineToolsAdmin.AppCfg
          end
        end
      
        def with_jars(*jars)  # :nodoc:
          failed = false
          begin
            yield
          rescue NameError => ex
            if failed
              raise ex
            else
              failed = true
              jars.each do |jar|
                $CLASSPATH << jar
              end
              retry
            end
          end
        end
      
        # Tries to find the Google App Engine SDK for Java.
        #
        # Looks for appcfg.sh in these directories (in order):
        # - ENV['APPENGINE_JAVA_SDK']/bin
        # - each directory in ENV['PATH']
        # - '/usr/local/appengine-java-sdk/bin'
        # - 'c:\appengine-java-sdk\bin'
        #
        # Returns File.join(sdk_directory, *pieces)
        #
        def sdk_path(*pieces)
          File.join(SDK_ROOT, *pieces)
        end
      end
    end
  end
end
